﻿using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

using Services;
using Domain;



namespace WebRole1
{


	public partial class Default : System.Web.UI.Page
	{

		

		public System.Drawing.Color Color1
		{
			get
			{
				return System.Drawing.Color.White;
			}
		}

		public System.Drawing.Color Color2
		{
			get
			{
				return System.Drawing.Color.FromArgb(238, 238, 238);
			}
		}



		protected void Page_Load(object sender, EventArgs e)
		{
#if DEBUG
			System.Diagnostics.Trace.TraceInformation("Default.aspx - Page_Load");
#endif

			Title = "Forum - Index";


			//Description and Key for the index page, used by Google and Bing
			HtmlMeta metaDesc = new HtmlMeta();
			metaDesc.Name = "description";
			metaDesc.Content = RoleEnvironment.GetConfigurationSettingValue("MetaDescContent");
			Page.Header.Controls.Add(metaDesc);

			HtmlMeta metaKey = new HtmlMeta();
			metaKey.Name = "keywords";
			metaKey.Content = RoleEnvironment.GetConfigurationSettingValue("MetaKeyContent");
			Page.Header.Controls.Add(metaKey);


			ForumService forumServ = new ForumService();
			IList<Forum> forums = forumServ.getForums();


			int backcolorType = 1;
			foreach (Forum f in forums)
			{
				TableRow tr = NewTableRow(backcolorType);

				TableCell tc = NewTableCell();
				tc.Width = Unit.Pixel(350);

				HyperLink lblForumName = new HyperLink();
				lblForumName.Font.Bold = true;
				lblForumName.NavigateUrl = "./ViewSubforum.aspx?id=" + f.ID.ToString();
				lblForumName.Text = f.Name;

				tc.Controls.Add(lblForumName);

				tr.Cells.Add(tc);


				tc = NewTableCell();
				tc.Width = Unit.Pixel(100);
				Label lblTopicCount = new Label();
				lblTopicCount.Text = f.Topiccount.ToString();
				tc.Controls.Add(lblTopicCount);
				tr.Cells.Add(tc);

				tc = NewTableCell();
				tc.Width = Unit.Pixel(100);
				Label lblPostcount = new Label();
				lblPostcount.Text = f.Postcount.ToString();
				tc.Controls.Add(lblPostcount);
				tr.Cells.Add(tc);

				tc = NewTableCell();
				tc.Width = Unit.Pixel(200);
				Label lblLastPost = new Label();
				lblLastPost.Text = f.LastPostOn.ToString();
				tc.Controls.Add(lblLastPost);
				tr.Cells.Add(tc);

				tblForums.Rows.Add(tr);


				TableRow trDesc = NewTableRow(backcolorType);

				TableCell tcDesc = NewTableCell();
				tcDesc.Width = Unit.Pixel(350);

				Label lblDesc = new Label();
				lblDesc.Text = f.Description;

				tcDesc.Controls.Add(lblDesc);

				trDesc.Cells.Add(tcDesc);

				tblForums.Rows.Add(trDesc);



				TableRow trEmpty = NewTableRow(backcolorType);
				trEmpty.Height = Unit.Pixel(10);
				TableCell tcEmpty = NewTableCell();
				tcEmpty.Width = Unit.Pixel(350);

				trEmpty.Cells.Add(tcEmpty);

				tblForums.Rows.Add(trEmpty);
			}
		}



		private void AddPostingContent(TableCell tc, string message, string PostedByName, string subtitle, int backcolorType)
		{
			IList<ContentPart> contentParts = ParseContent(message);

			tc.BorderStyle = BorderStyle.None;

			Table tbl = NewTable();





			foreach (ContentPart part in contentParts)
			{
				TableRow row = NewTableRow(backcolorType);
				row.Width = Unit.Pixel(700);
				row.HorizontalAlign = HorizontalAlign.Justify;




				if (part.Type == "quote")
				{
					TableRow rowQuote = NewTableRow(backcolorType);
					rowQuote.Width = Unit.Pixel(700);
					rowQuote.HorizontalAlign = HorizontalAlign.Justify;

					//Message content
					TableCell cell = NewTableCell();
					cell.Width = Unit.Pixel(50);

					//Label lblQuote = new Label();
					//lblQuote.Text = "Niks";
					//cell.Controls.Add(lblQuote);
					rowQuote.Cells.Add(cell);



					cell = NewTableCell();


					Label lblQuote = new Label();
					lblQuote.Text = "quote:";
					lblQuote.Font.Size = FontUnit.Point(8);
					cell.Controls.Add(lblQuote);
					rowQuote.Cells.Add(cell);

					tbl.Rows.Add(rowQuote);




					rowQuote = NewTableRow(backcolorType);
					rowQuote.Width = Unit.Pixel(700);
					rowQuote.HorizontalAlign = HorizontalAlign.Justify;

					cell = NewTableCell();
					cell.Width = Unit.Pixel(50);
					rowQuote.Cells.Add(cell);


					//Message content
					cell = NewTableCell();
					cell.Width = Unit.Pixel(50);
					rowQuote.Cells.Add(cell);

					Label lblMessage = new Label();
					lblMessage.Text = part.Content;

					cell.Controls.Add(lblMessage);

					rowQuote.Cells.Add(cell);

					tbl.Rows.Add(rowQuote);


				}
				else
				{
					//Message content
					TableCell cell = NewTableCell();
					cell.ColumnSpan = 2;

					Label lblMessage = new Label();
					lblMessage.Text = part.Content;

					cell.Controls.Add(lblMessage);

					row.Cells.Add(cell);

					tbl.Rows.Add(row);
				}
			}






			//row = NewTableRow(backcolorType);
			//row.Width = Unit.Pixel(700);
			//row.HorizontalAlign = HorizontalAlign.Justify;



			////Message content
			//cell = NewTableCell();
			//cell.ColumnSpan = 4;
			//cell.RowSpan = 3;

			//lblMessage = new Label();
			//lblMessage.Text = message;

			//cell.Controls.Add(lblMessage);

			//row.Cells.Add(cell);

			//tbl.Rows.Add(row);





			tc.Controls.Add(tbl);
		}



		private IList<ContentPart> ParseContent(string content)
		{
			while (content.Contains("[url=") && content.Contains("[/url]"))
			{
				int indexOfStartQuote = content.IndexOf("[url=");
				int indexOfEndQuote = content.IndexOf("[/url]");

				string newContent = content.Substring(0, indexOfStartQuote);

				string url = content.Substring(indexOfStartQuote, indexOfEndQuote - indexOfStartQuote);
				url = url.Replace("[url=", "<A HREF=");
				url = url.Replace("]", ">");
				url = url.Replace("[/url]", "</A>");

				content = newContent + url;
			}


			IList<ContentPart> contentParts = new System.Collections.Generic.List<ContentPart>();

			if (content.Contains("[quote]"))
			{
				if (content.Contains("[/quote]"))
				{
					int indexOfStartQuote = content.IndexOf("[quote]");
					int indexOfEndQuote = content.IndexOf("[/quote]");

					if (indexOfStartQuote > 0)
					{
						ContentPart firstPart = new ContentPart("msg", content.Substring(0, indexOfStartQuote));
						contentParts.Add(firstPart);

						ContentPart quotePart = new ContentPart("quote", content.Substring(indexOfStartQuote + 7, indexOfEndQuote - indexOfStartQuote - 7));
						contentParts.Add(quotePart);

						ContentPart restPart = new ContentPart("msg", content.Substring(indexOfEndQuote + 8, content.Length - indexOfEndQuote - 1 - 7));
						contentParts.Add(restPart);
					}
					else
					{
						ContentPart quotePart = new ContentPart("quote", content.Substring(0, indexOfEndQuote));
						contentParts.Add(quotePart);

						ContentPart restPart = new ContentPart("msg", content.Substring(indexOfEndQuote, content.Length - indexOfEndQuote - 1 - 8));
						contentParts.Add(restPart);
					}
				}
			}
			else
			{
				ContentPart cp = new ContentPart("msg", content);
				contentParts.Add(cp);
			}



			return contentParts;
		}




		private Table NewTable()
		{
			Table tbl = new Table();
			tbl.CellPadding = 0;
			tbl.CellSpacing = 0;

			return tbl;
		}


		private TableRow NewTableRow(int backcolorType)
		{
			TableRow row = new TableRow();
			row.BorderStyle = BorderStyle.None;
			row.Width = Unit.Pixel(700);
			row.HorizontalAlign = HorizontalAlign.Justify;


			if (backcolorType == 1)
			{
				row.BackColor = Color1;
				row.BorderColor = Color1;

			}
			else if (backcolorType == 2)
			{
				row.BackColor = Color2;
				row.BorderColor = Color2;
			}

			return row;
		}


		private TableCell NewTableCell()
		{
			TableCell cell = new TableCell();
			cell.BorderStyle = BorderStyle.None;
			return cell;
		}






		void lblEdit_Command(object sender, CommandEventArgs e)
		{
			Response.Redirect("Profile.aspx?id=" + e.CommandArgument.ToString());
		}



		private class ContentPart
		{
			public ContentPart(string type, string content)
			{
				Type = type;
				Content = content;
			}

			public string Type { get; set; }
			public string Content { get; set; }
		}
	}
}